﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Abp.Dapper.Repositories;
using Abp.Data;
using Abp.Domain.Uow;
using Dapper;
using GCT.Foundation.EntityFrameworkCore;
using L._52Abp.Database;
using L._52AbpPro.Roles;

namespace GCT.Foundation.Database
{

    public class FoundationTemplateDapperRepository : DapperEfRepositoryBase<FoundationTemplateDbContext, Role>
    {
        protected readonly IActiveTransactionProvider ActiveTransactionProvider;

        public FoundationTemplateDapperRepository(
            IActiveTransactionProvider activeTransactionProvider,
            ICurrentUnitOfWorkProvider currentUnitOfWorkProvider)
            : base(
                activeTransactionProvider,
                currentUnitOfWorkProvider)
        {
            ActiveTransactionProvider = activeTransactionProvider;
        }
    }

    public class FoundationTemplateSqlExecutor : ISqlExecutor
    {
        private readonly FoundationTemplateDapperRepository _dapperRepository;

        public FoundationTemplateSqlExecutor(FoundationTemplateDapperRepository dapperRepository)
        {
            _dapperRepository = dapperRepository;
        }

        public IEnumerable<TAny> Query<TAny>(string query, object parameters = null)
            where TAny : class
        {
            return _dapperRepository.Query<TAny>(query, parameters);
        }

        public async Task<IEnumerable<TAny>> QueryAsync<TAny>(string query, object parameters = null)
            where TAny : class
        {
            return await _dapperRepository.QueryAsync<TAny>(query, parameters);
        }

        public int Execute(string query, object parameters = null)
        {
            return _dapperRepository.Execute(query, parameters);
        }

        public async Task<int> ExecuteAsync(string query, object parameters = null)
        {
            return await _dapperRepository.ExecuteAsync(query, parameters);
        }

        public async Task<IDataReader> ExecuteReaderAsync(string query, object parameters = null)
        {
            var connection = await this.GetConnectionAsync();
            return await connection.ExecuteReaderAsync(query, parameters, await this.GetActiveTransactionAsync(),
                _dapperRepository.Timeout);
        }

        public IDbConnection GetConnection()
        {
            return this._dapperRepository.GetConnection();
        }

        public async Task<IDbConnection> GetConnectionAsync()
        {
            return await this._dapperRepository.GetConnectionAsync();
        }

        public IDbTransaction GetActiveTransaction()
        {
            return this._dapperRepository.GetActiveTransaction();
        }

        public async Task<IDbTransaction> GetActiveTransactionAsync()
        {
            return await this._dapperRepository.GetActiveTransactionAsync();
        }
    }
}
